//
//  LLModificationViewController.m
//  UserUse
//
//  Created by Yiwi on 16/10/17.
//  Copyright © 2016年 CJPC. All rights reserved.
//

#import "LLModificationViewController.h"
#import "LLModificationView.h"
#import "HomePageViewController.h"
#import "BaseNavigationController.h"
@interface LLModificationViewController ()<LLModificationViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
    LLModificationView * _modificationView;
    UIImage * _image;
    NSData *_imageData;
}

@end

@implementation LLModificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"完善个人信息";
    [self setupSubView];
}

-(void)setupSubView{
    _modificationView = [LLModificationView new];
    _modificationView.frame = kScreenFrame;
    _modificationView.delegate = self;
    [self.view addSubview:_modificationView];
    [_modificationView setupSubView];
#warning  代写逻辑：先从缓存取判断有无登陆记录
    [_modificationView setupNoLoginView];

}

#pragma mark-LLModificationViewDelegate
//打开本地相册
-(void)pickPhoto{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}
//提交保存
-(void)commitUserName:(NSString *)username withUserCarNum:(NSString *)userCarNum{
    if (_image == nil) {
        MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hub.mode = MBProgressHUDModeText;
        hub.labelText = @"未选择头像";
        [hub showAnimated:YES whileExecutingBlock:^{
            sleep(1);
        }completionBlock:^{
            [hub removeFromSuperview];
        }];
        return;
    }
    if (username == nil ||[username isEqualToString:@""]) {
        MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hub.mode = MBProgressHUDModeText;
        hub.labelText = @"请输入名字";
        [hub showAnimated:YES whileExecutingBlock:^{
            sleep(1);
        }completionBlock:^{
            [hub removeFromSuperview];
        }];
        return;
    }
    
    if (userCarNum == nil ||[userCarNum isEqualToString:@""]) {
        MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hub.mode = MBProgressHUDModeText;
        hub.labelText = @"请输入车牌号";
        [hub showAnimated:YES whileExecutingBlock:^{
            sleep(1);
        }completionBlock:^{
            [hub removeFromSuperview];
        }];
        return;
    }
    [AllProjectRequest commintModificationWithUserName:username WithUserCarNum:userCarNum WithImage:_image success:^(id responseObject) {
        if(_isChange)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            HomePageViewController *mainVC = [[HomePageViewController alloc]init];
            BaseNavigationController *baseVC = [[BaseNavigationController alloc]initWithRootViewController:mainVC];
            //            mainVC.isDir = YES;
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            window.rootViewController = baseVC;
        }
    } failure:^(NSError *error) {
    
    }];
}

#pragma mark-UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        _image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        _image = [UIImage compressImage:_image toTargetWidth:180];
        if (UIImagePNGRepresentation(_image) == nil)
        {
            _imageData = UIImageJPEGRepresentation(_image, 1.0);
        }
        else
        {
            _imageData = UIImagePNGRepresentation(_image);
        }
        //图片保存的路径
        //这里将图片放在沙盒的documents文件夹中
        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        //文件管理器
        NSFileManager *fileManager = [NSFileManager defaultManager];
        //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/header.png"] contents:_imageData attributes:nil];
        //得到选择后沙盒中图片的完整路径
        NSString * filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/header.png"];
        NSLog(@"沙盒路径：%@",filePath);
        //关闭相册界面
        [picker dismissViewControllerAnimated:YES completion:^{
            [_modificationView.iconButton setImage:_image forState:UIControlStateNormal];
        }];
    }
}


@end
